package com.imessage.action.business.wuma;

import com.bean.BusinessWuma;
import com.imessage.action.BaseAction;
import com.imessage.comm.Constants;
import com.imessage.type.BusinessAppleState;
import com.imessage.type.BusinessWumaState;
import com.imessage.utils.DateUtil;
import com.imessage.utils.StrUtil;
import com.imessage.vo.WumaVo;
import io.ebean.Expr;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Slf4j
public class WumaBaseAction extends BaseAction {

    protected final String datagrid = "datagrid-businessWuma-filter";

    protected void setExpressionList(List<Expression> expressionList, WumaVo wumaVo) {
        if (StringUtils.isNotBlank(wumaVo.getMac())) {
            expressionList.add(Expr.contains("mac", wumaVo.getMac()));
        }
        if (StringUtils.isNotBlank(wumaVo.getAppleId())) {
            expressionList.add(Expr.eq("BusinessApple.appleId", wumaVo.getAppleId()));
        }
        if (StringUtils.isNotBlank(wumaVo.getState())) {
            expressionList.add(Expr.eq("state", wumaVo.getState()));
        }
    }


    protected void setModelAndView(ModelAndView modelAndView, BusinessWuma businessWuma) {
        if(businessWuma.getId()==null||businessWuma.getId().intValue()==0){
            businessWuma.setState(BusinessWumaState.init);
        }
        modelAndView.addObject("businessWumaForm", businessWuma);
        modelAndView.addObject("statusList", BusinessWumaState.values());
    }

    protected void setWuma(WumaVo wumaVo, BusinessWuma businessWuma) {
        copyProperties(wumaVo, businessWuma);
        if (StringUtils.isNotBlank(wumaVo.getState())) {
            businessWuma.setState(BusinessWumaState.fromValue(wumaVo.getState()));
        }else{
            businessWuma.setState(BusinessWumaState.init);
        }
    }

    protected String checkData(WumaVo wumaVo, BusinessWuma businessWuma) {
        BusinessWuma wuma=baseService.findObject(BusinessWuma.class, Expr.eq("mac", wumaVo.getMac()));
        if(wuma==null){
            return "";
        }
        if(businessWuma==null){
            return "mac已存在";
        }
        if(wuma.getMac().equalsIgnoreCase(businessWuma.getMac().toLowerCase())){
            return "";
        }
        return "mac已存在";
    }


    protected boolean saveExcel(String filePath){
        try {
            File file = new File(filePath);
            InputStream is = new FileInputStream(file);
            String fileName = file.getName();
            Workbook hssfWorkbook = null;
            if (fileName.endsWith("xlsx")){
                hssfWorkbook = new XSSFWorkbook(is);	//Excel 2007
            }else if (fileName.endsWith("xls")){
                hssfWorkbook = new HSSFWorkbook(is);	//Excel 2003
            }
            // 循环工作表Sheet
            for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
                Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
                if (hssfSheet == null) {
                    continue;
                }
                List<String> headlist = new ArrayList<String>();
                Row row0 = hssfSheet.getRow(0);// 第一张sheet的第一行
                if (row0 == null) {
                    continue;
                }
                for(int cellNum=0;cellNum<row0.getLastCellNum();cellNum++){
                    Cell cell = row0.getCell(cellNum);
                    headlist.add(cell.getStringCellValue().trim());// 读取
                }
                // 循环行Row
                for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                    Row row = hssfSheet.getRow(rowNum);
                    if (row == null) {
                        continue;
                    }
                    BusinessWuma businessWuma = new BusinessWuma();
                    for (int j = 0; j < headlist.size(); j++) { // 对每个单元格进行循环
                        String value = headlist.get(j);// 读取当前单元格的值
                        Cell cell = row.getCell(j);
                        if(cell==null||StringUtils.isBlank(cell.getStringCellValue())){
                            continue;
                        }
                        String content = cell.getStringCellValue().trim();
                        if(value.toLowerCase().indexOf("mac")!=-1){
                            businessWuma.setMac(content.toUpperCase());
                        }
                        if(value.toLowerCase().indexOf("mlb")!=-1){
                            businessWuma.setMlb(content);
                        }
                        if(value.toLowerCase().indexOf("serial")!=-1){
                            businessWuma.setSerialNum(content);
                        }
                        if(value.toLowerCase().indexOf("board")!=-1){
                            businessWuma.setBoardId(content);
                        }
                        if(value.toLowerCase().indexOf("product")!=-1){
                            businessWuma.setProductName(content);
                        }
                    }
                    BusinessWuma wuma=wumaService.findBusinessWuma(businessWuma.getMac());
                    if(wuma==null){
                        businessWuma.setState(BusinessWumaState.init);
                        baseService.saveObject(businessWuma);
                    }
                }
            }
        } catch (Exception e) {
            log.error("", e);
            return false;
        }
        return true;
    }


}
